package work.lrl.qyuserJsonToCsv;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong.Yang
 */
public class Demo {
    private final static String prefix = "E:\\work-records\\z照相机\\s-lrl企微json转csv\\原始材料\\LRL企微用户标签";

    private final static String filePath = prefix + ".json";

    private final static String excelName = prefix + ".xlsx";

    public static void main(String[] args) {
        File file = new File(filePath);
        String jsonString = FileUtil.readString(file, Charset.defaultCharset());
        JSONObject jsonObject = JSONUtil.parseObj(jsonString);

        List<D> list = new ArrayList<>();

        JSONArray tagGroup = jsonObject.getJSONArray("tag_group");
        for (int i = 0; i < tagGroup.size(); i++) {
            JSONObject item = tagGroup.get(i, JSONObject.class);
            String groupName = item.get("group_name", String.class);

            JSONArray tag = item.getJSONArray("tag");
            for (int j = 0; j < tag.size(); j++) {
                JSONObject tagItem = tag.get(j, JSONObject.class);
                String tagName = tagItem.get("name", String.class);
                list.add(new D(groupName, tagName));
            }
        }

        EasyExcel.write(excelName).sheet("sheet1").doWrite(list);
    }

    @Data
    @AllArgsConstructor
    static class D {
        private String groupName;
        private String tagName;
    }
}
